package com.renren.youyou.utils;

import java.util.ArrayList;
import java.util.List;
import java.util.Observer;

public class LogOutObservable{
	
	private LogOutObservable() {
	}
	
	private static LogOutObservable mInstrance;
	
	public static LogOutObservable getInstrance(){
		if(mInstrance == null){
			mInstrance = new LogOutObservable();
		}
		return mInstrance;
	}

	List<LogOutListener> observers = new ArrayList<LogOutListener>();
	
	public void addObserver(LogOutListener observer) {
        if (observer == null) {
            throw new NullPointerException();
        }
        synchronized (this) {
            if (!observers.contains(observer))
                observers.add(observer);
        }
    }
	
    public synchronized void deleteObserver(LogOutListener observer) {
        observers.remove(observer);
    }
    
    public synchronized void deleteObservers() {
        observers.clear();
    }
    
    public void notifyObservers() {
        int size = 0;
        LogOutListener[] arrays = null;
        synchronized (this) {
            size = observers.size();
            arrays = new LogOutListener[size];
            observers.toArray(arrays);
        }
        if (arrays != null) {
            for (LogOutListener observer : arrays) {
                observer.onLogout();
            }
        }
    }
	
}
